<?php

class Web_Admin_Productos_Svc_GuardarProducto
{

    public function doIt()
    {
        $this->guardar();
    }

    private function guardar()
    {
        $obj = new Web_Db_Productos();
        $rs = $obj->fetchRow($obj->select()
                                ->where('pro_nombre=?', $_POST['pro_nombre']));

        if ($_POST['pro_nombre'] == '') {
            $error['pro_nombre'] = 'Ingrese Nombre';
        } else {
            if (count($rs) > 0) {
                $error['pro_nombre'] = 'El Producto ya se ha registrado';
            }
        }
        if ($_POST['pro_descripcion'] == '') {
            $error['pro_descripcion'] = 'Ingrese Descripcion';
        }
        if ($_POST['pro_precio_s'] == '') {
            $error['pro_precio_s'] = 'Ingrese Precio Soles';
        }
        $handle = new Ey_Upload($_FILES['imagen']);
        if (!$handle->uploaded) {
            $error['imagen'] = 'Debe indicar la ruta del archivo';
        }
        if (count($error) > 0) {
            $_SESSION['post'] = $_POST;
            $_SESSION['error'] = $error;
            Ey::redirect($_SERVER['HTTP_REFERER']);
        }

        $pro_key = str_replace('’', '', $_POST['pro_nombre']);
        $pro_keye = str_replace("'", "", $pro_key);
        $pro_keyi = str_replace("/", "", $pro_keye);

        $stranges = array('&aacute;', '&eacute;', '&iacute;', '&oacute;', '&uacute;', '&Aacute;', '&Eacute;', '&Iacute;', '&Oacute;', '&Uacute;', '&ntilde;', '&Ntilde;');

        $fixed = array('á', 'é', 'í', 'ó', 'ú', 'Á', 'É', 'Í', 'Ó', 'Ú', 'ñ', 'Ñ');

        $_POST['pro_descripcion'] = str_replace($stranges, $fixed, $_POST['pro_descripcion']);
        
        $_POST['pro_restriccion'] = str_replace($stranges, $fixed, $_POST['pro_restriccion']);

        $row = array('pro_nombre' => $_POST['pro_nombre'],
                    'pro_descripcion' => $_POST['pro_descripcion'],
                    'pro_restriccion' => $_POST['pro_restriccion'],
                    'pro_precio' => $_POST['pro_precio_s'],
                    'pro_estado' => 1,
                    'pro_fecha' => date('Y-m-d'),
                    'pro_destacar' => 0,
                    'pro_relacionado' => 0,
                    'pro_key' => Ey::urlAmigable($pro_keyi));

        $obj->insert($row);

        $id = $obj->getAdapter()->lastInsertId();

        if ($handle->uploaded) {
            $handle->file_new_name_body = 'pro_' . $id;
            $handle->file_new_name_ext = 'jpg';
            $handle->image_resize = true;
            $handle->image_ratio_crop = true;
            $handle->image_x = 300;
            $handle->image_y = 300;
            $handle->process(APP_ROOT . DS . '_data/img/productos');
//            $handle->clean();
        }
        
        $handleSmall = new Ey_Upload($_FILES['imagen']);
        if ($handleSmall->uploaded) {
            $handleSmall->file_new_name_body = 'pro_' . $id . '_small';
            $handleSmall->file_new_name_ext = 'jpg';
            $handleSmall->image_resize = true;
            $handleSmall->image_ratio_crop = true;
            $handleSmall->image_x = 150;
            $handleSmall->image_y = 150;
            $handleSmall->process(APP_ROOT . DS . '_data/img/productos');
            $handleSmall->clean();
        }
        
        $handle2 = new Ey_Upload($_FILES['img2']);
        if ($handle2->uploaded) {
            $handle2->file_new_name_body = 'pro_' . $id . '_2';
            $handle2->file_new_name_ext = 'jpg';
            $handle2->image_resize = true;
            $handle2->image_ratio_crop = true;
            $handle2->image_x = 300;
            $handle2->image_y = 300;
            $handle2->process(APP_ROOT . DS . '_data/img/productos');
            $handle2->clean();
        }
        
        $handle3 = new Ey_Upload($_FILES['img3']);
        if ($handle3->uploaded) {
            $handle3->file_new_name_body = 'pro_' . $id . '_3';
            $handle3->file_new_name_ext = 'jpg';
            $handle3->image_resize = true;
            $handle3->image_ratio_crop = true;
            $handle3->image_x = 300;
            $handle3->image_y = 300;
            $handle3->process(APP_ROOT . DS . '_data/img/productos');
            $handle3->clean();
        }
        
        $handle4 = new Ey_Upload($_FILES['img4']);
        if ($handle4->uploaded) {
            $handle4->file_new_name_body = 'pro_' . $id . '_4';
            $handle4->file_new_name_ext = 'jpg';
            $handle4->image_resize = true;
            $handle4->image_ratio_crop = true;
            $handle4->image_x = 300;
            $handle4->image_y = 300;
            $handle4->process(APP_ROOT . DS . '_data/img/productos');
            $handle4->clean();
        }

        Ey::redirect(WEB_ROOT . '/admin/productos');
    }

}

